package com.odm.communitymanage.ui.DoorSafe

import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ResourceUtils
import com.blankj.utilcode.util.SPUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.jeremyliao.liveeventbus.LiveEventBus
import com.odm.communitymanage.R
import com.odm.communitymanage.base.BaseMVVMFragment
import com.odm.communitymanage.core.Constants
import com.odm.communitymanage.ui.DoorSafe.InviteVisitorFragment
import com.odm.communitymanage.ui.notifications.NoticesFragment
import kotlinx.android.synthetic.main.fragment_door_card_list.*

/**
 * @description: 门禁卡列表
 * @author: ODM
 * @date: 2020/4/12
 */
class DoorCardListFragment : BaseMVVMFragment(){

    lateinit var adapter: DoorCardAdapter
    var cardList: MutableList<DoorSafeCard> = mutableListOf()
    
    val communityName :String = SPUtils.getInstance(Constants.SP_USER).getString(Constants.USER_COMMUNITY_NAME , "")

    init {
        cardList.add(DoorSafeCard(0,"东门门禁卡",communityName,Integer.MAX_VALUE))
        cardList.add(DoorSafeCard(0,"北门门禁卡",communityName,Integer.MAX_VALUE))
        cardList.add(DoorSafeCard(1,"3栋7单元门禁卡",communityName,Integer.MAX_VALUE))
        cardList.add(DoorSafeCard(2,"西7 305 房卡",communityName,-1))
    }

    override fun initViewModelObserve() {
    }

    override fun initViews() {
        //左上角返回 事件
        btnBackCardList.setOnClickListener {
            pop()
        }
        //发起邀请 事件
        tvInviteVisitor.setOnClickListener {
            start(InviteVisitorFragment())
        }
        rvCardList.layoutManager = LinearLayoutManager(requireContext())
        adapter = DoorCardAdapter(cardList)
        rvCardList.adapter = adapter
        adapter.setOnItemClickListener { adapter1, view, position ->
            LiveEventBus.get("fake_card_detail")
                .post(adapter.data[position].door_name)
            start(CardDetailFragment())
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_door_card_list
    }

    data class DoorSafeCard(
        //0 小区门  1 楼道门  2 屋外门
        val type : Int,
        val door_name : String,
        val community_name : String ,
        val rest_time : Int
    )

    class DoorCardAdapter (data : MutableList<DoorSafeCard>)
        : BaseQuickAdapter<DoorSafeCard, BaseViewHolder>(R.layout.item_card_list , data) {

        override fun convert(holder: BaseViewHolder, item: DoorSafeCard) {
            when(item.type){
                0 ->{
                    holder.setImageResource(R.id.ivTypeCard , R.mipmap.ic_community_door )
                }

                1->{
                    holder.setImageResource(R.id.ivTypeCard , R.mipmap.ic_building_door )
                }

                2->{
                    holder.setImageResource(R.id.ivTypeCard , R.mipmap.ic_room_door )
                }
            }

            holder.setText(R.id.tvNameCard , item.door_name)
            holder.setText(R.id.tvNameCommunity , item.community_name)
            if(item.rest_time >= 0){
                //有限制次数
                val rest = if(item.rest_time < Int.MAX_VALUE){
                    item.rest_time.toString()+" 次"
                }else {
                    "∞ 次"
                }
                holder.setText(R.id.tvLastCount , rest)
                holder.setVisible(R.id.tvLastEnter ,true)
                holder.setVisible(R.id.tvLastCount ,true)
            }else {
                holder.setVisible(R.id.tvLastEnter ,false)
                holder.setVisible(R.id.tvLastCount ,false)
            }

        }
    }

    class DoorCardDetail
}